package com.zkhz.base.security.filter;

import com.zkhz.base.security.token.TokenValueHolder;
import com.zkhz.base.security.util.BearerTokenUtil;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 保存TokenValue的拦截器，配置在spring security中。
 *
 * @author wuzeqin
 * @date 2020/6/9
 **/
public class TokenValueFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            TokenValueHolder.setTokenValue(BearerTokenUtil.extractToken(request));
            filterChain.doFilter(request, response);
        }
        finally {
            TokenValueHolder.remove();
        }
    }


}
